/**
 * Created by LYK on 2017/4/7.
 */

$(function () {



	//获取该页面的modelCode
	//var frameId = self.frameElement.getAttribute('id');
	//console.log(parent.sysObj[frameId].modelCode);


	//项目根路径
	var basePath=$("#txtRootPath").val();

	var height = window.parent.document.getElementById('iframe_box').offsetHeight-100;

	// var table = new tableInit();
	// table.Init();
	$('#department-table').bootstrapTable({
		url			:basePath+'/getDepartmentList',
		method		:'post',
		// toolbar		:'#',			//工具栏
		striped		:true,					//是否显示间隔栏颜色
		cache		:false,					//是否使用缓存，默认为true
		pagination	:true,					//是否分页
		sortable	:false,					//是否启用排序
		pageSize	:5,
		pageList	:[5,10,15,20],
		height		:height,

		columns		:[{
			title:'选中',
			field:'state',
			checkbox:true,
		},{
			field:'NO',
			title:'序号',
			align:'center',
			width:40
		},{
			field:'FCODE',
			title:'部门代码',
			align:'center',
			width:100
		},{
			field:'FNAME',
			title:'部门名称',
			width:500,

		},{
			field:'FPARENT',
			title:'所属机构',
			width:150,
			align:'center'
		},{
			field:'FDEPTLEVEL',
			title:'部门级次',
			width:80,
			align:'center'
		},{
			field:'FISSTOP',
			title:'停用',
			width:50,
			align:'center'
		}],
		onDblClickCell:function (field,value,row,td) {
			edit(row,'department-table');
		},
		onLoadSuccess:function (data) {
		},
		onLoadError:function (data) {
			tsUtils.handleError(data);
		}

	})
});

function onRefresh() {
    $('#department-table').bootstrapTable('refresh');
}
/*增加部门*/
function addDepartment() {
	$('#add-content form :input').attr('value','');
	layer.open({
		type:1,
		title:'添加部门',
		area:['600px','400px'],
		skin:'layer-window',
		shadeClose:true,
		content:$('#add-content'),
		btn:['添加','取消'],
		yes:function () {
			postData();
		},
	})
}

function postData() {
	var values = $('#add-content >form').serializeArray();
	var data = {};
	for(var i = 0 ; i < values.length; i ++){
		var object = values[i];
		if(object.name == 'parent-department'){
			data[object.name] = $('#parent-department').attr('fcode');
		}else if(object.name == 'parent-department'){
			if(object.value == 'on'){
				data[object.name] = '1';
			}else {
				data[object.name] = '0';
			}
		}else{
			data[object.name] = object.value;
		}
	}
	$.ajax({
		type:'post',
		url:'/addDepartment',
		data:JSON.stringify(data),
		contentType:'application/json;charset=utf-8',
		success:function (data) {
		},
		error:function (data) {
		},
		complete:function (data) {
		}
	})
}

/*删除部门*/
function removeDepartment(tableId) {
	var selectArray;
	selectArray = $('#' + tableId).bootstrapTable('getSelections');
	if(selectArray.length == 0){
		layer.alert('请选择删除项',{icon:6})
	}else{
		layer.alert('确定要删除这'+selectArray.length+'项吗？',{
			time:0,
			btn:['确定','取消'],
			yes:function (index) {
				var dataArray = [];
				for(var i = 0 ; i < selectArray.length ;  i++ ){
					var object = selectArray[i];
					dataArray.push(object.FCODE);
				}
				$.ajax({
					type:'post',
					url:'/deleteDepartment',
					data:JSON.stringify(dataArray),
					contentType:'application/json;charset=utf-8',
					success:function (data) {
						if(data.msg == 'success'){
							layer.close(index);
							layer.msg('删除成功',{time:1500});
						}else{
							layer.close(index);
							layer.msg('删除失败',{time:1500});
						}
					},
					error:function (data) {

					}
				});
			}
		})
	}
}

/*修改部门*/
function editDepartment(tableId) {
	var selectArray;
	selectArray = $('#' + tableId).bootstrapTable('getSelections');
	if(selectArray.length == 0){
		layer.alert('请选择修改项',{icon:6})
	}else if(selectArray.length > 1){
		layer.alert('只能选择一项修改',{icon:6})
	}else{
		$('#add-content form :input').attr('value','');
		var object = selectArray[0];
		setForm(object);
		layer.open({
			type:1,
			title:'添加部门',
			area:['600px','400px'],
			skin:'layer-window',
			shadeClose:true,
			content:$('#add-content'),
			btn:['修改','取消'],
			yes:function () {

			}
		})
	}
}

function edit(data) {
	$('#add-content form :input').attr('value','');
	setForm(data);
	layer.open({
		type:1,
		title:'添加部门',
		area:['600px','400px'],
		skin:'layer-window',
		shadeClose:true,
		content:$('#add-content'),
		btn:['修改','取消'],
		yes:function () {

		}
	})
}

function setForm(data) {
	$('#department-code').attr('value',data.code);
	$('#department-name').attr('value',data.name);
	$('#parent-department').attr('value',data.parent);
}

//选择父机构
function chooseParent() {
	layer.open({
		// offset:'t',
		title:'父机构列表',
		area:['500px','300px'],
		content:'<div id = "parent-tree" class="ztree"></div>',
		success:function () {
			var setting = {
				async:{
					enable:true,
					url:tsUtils.getRootPath()+'/getParentDepartment',
					contentType:'application/json',
					dataType:"text",
					autoParam:[]
				},
				data:{
					key:{
						name:"FNAME",
					},
					simpleData:{
						enable:true,
						idKey:"FCODE",
						pIdKey:"FPARENT",
						rootPID:9999
					}
				},
				callback:{
					onAsyncSuccess:onAsyncSuccess,
					onAsyncError:onAsyncError
				}
			};
			$.fn.zTree.init($('#parent-tree'),setting);
		}
	})
}

function onAsyncSuccess(event,treeId,treeNode,msg) {

}

function onAsyncError(event,treeId,treeNode,XMLHttpRequest,textStatus,errorThrown) {
	tsUtils.handleError(XMLHttpRequest.responseText);
}